

//
//  DayReportContentViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/28.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class DayReportContentViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,MBProgressHUDDelegate {
    
    var tableView: UITableView!
    
    var headArray = NSMutableArray()
    var reportArray = NSMutableArray()
    
    var header1: UIView!
    var header2: UIView!
    
    var dic: JSON!
    
    var hud: MBProgressHUD!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        InitHeaderView()
        InitViews()
        InitData()
    }
    
    /**
     初始化tableview的分隔
     */
    func InitHeaderView() {
        header1 = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: 50))
        header1.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        
        let icon = UIImageView.init(frame: CGRect(x: 10, y: 8, width: 27, height: 34))
        icon.layer.cornerRadius = 15
        icon.image = logo
        icon.contentMode = UIViewContentMode.scaleAspectFit
        let title = UILabel.init(frame: CGRect(x: icon.frame.maxX + 10, y: 10, width: screenW - 60, height: 30))
        title.font = UIFont.boldSystemFont(ofSize: 16)
        title.textColor = UIColor.darkGray
        title.text = "访问情况汇总"
        let line = UIView.init(frame: CGRect(x: 40, y: 49, width: screenW, height: 1))
        line.backgroundColor = UIColor.init(hexString: "f0f0f0", alpha: 1)
        header1.addSubview(icon)
        header1.addSubview(title)
        header1.addSubview(line)
        
        header2 = UIView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: 50))
        header2.backgroundColor = UIColor.init(hexString: "eeeeee", alpha: 1)
        let icon1 = UIImageView.init(frame: CGRect(x: 10, y: 8, width: 27, height: 34))
        icon1.layer.cornerRadius = 15
        icon1.image = logo
        icon1.contentMode = UIViewContentMode.scaleAspectFit
        let lable = UILabel.init(frame: CGRect(x: icon.frame.maxX + 10, y: 10, width: screenW - 60, height: 30))
        lable.font = UIFont.boldSystemFont(ofSize: 16)
        lable.textColor = UIColor.darkGray
        lable.text = "\(appname)数据汇总"
        header2.addSubview(icon1)
        header2.addSubview(lable)
    }
    /**
     初始化控件
     */
    func InitViews() {
        
        tableView = UITableView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH))
        tableView.backgroundColor = UIColor.white
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
//        tableView.separatorColor = UIColor.init(hexString: "f0f0f0", alpha: 1)
//        tableView.separatorInset = UIEdgeInsetsMake(0, 40, 0, 0)
        self.view.addSubview(tableView)
        
//        setUpRefresh()
    }
    
    func InitData() {
        
        hud = MBProgressHUD.showAdded(to: self.view, animated: true)
        hud.animationType = MBProgressHUDAnimation.fade
        
        let queue = DispatchQueue(label: "queue")
        queue.async {
            let useNum = self.dic["useNum"].stringValue //昨日使用人数
            
            let fwcon = self.dic["fwcon"].stringValue//昨日访问量
            let strper = self.dic["strper"].stringValue//活跃率
            let fampracon = self.dic["fampracon"].stringValue//家长点赞数
            let famrecon = self.dic["famrecon"].stringValue//家长评论数
            let userpracon = self.dic["userpracon"].stringValue//所有点赞数
            let userRecon = self.dic["userRecon"].stringValue//所有评论数
            var edittime = self.dic["edittime"].stringValue
            edittime = edittime.components(separatedBy: "T")[0]
            self.title = edittime
            let classactcon = self.dic["classactcon"].intValue//学校当日班级活动发布的数量
            let honorcon = self.dic["honorcon"].intValue//学校当日班级荣誉发布数量
            let anncon = self.dic["AnnCon"].intValue//学校当日班级通知发布数量
            let growthcon = self.dic["GrowthCon"].intValue//学校当日成长记录发布数量
            let leavecon = self.dic["LeaveCon"].intValue//学校当日请假数量
            let HomWorkCon = self.dic["HomWorkCon"].intValue //学校当日作业数量
            
            
//            let count = classactcon! + honorcon! + anncon! + growthcon! + leavecon! + HomWorkCon!
            let users = "访问量 \(fwcon)次，访问人数 \(useNum) 人"
            
            let allTeacher = "活跃率 \(strper)%"
            let signuser = "家长点赞\(fampracon)次，评论\(famrecon)次"
            let allComment = "总点赞\(userpracon)次，评论\(userRecon)次"
//            let allSend = "昨日总发布数量\(count)个"
            self.headArray.add(users)
            self.headArray.add(allTeacher)
            self.headArray.add(signuser)
            
            self.headArray.add(allComment)
//            self.headArray.addObject(allSend)
            
            
            let classwork = "班级活动发布\(classactcon)次"
            let classhornor = "班级荣誉发布\(honorcon)次"
            let classnotice = "班级通知发布\(anncon)次"
            let growth = "成长记录发布\(growthcon)次"
            let leave = "请假次数\(leavecon)人"
            let homework = "作业备忘发布\(HomWorkCon)次"
            
            self.reportArray.add(classwork)
            self.reportArray.add(classnotice)
            self.reportArray.add(classhornor)
            self.reportArray.add(growth)
            self.reportArray.add(leave)
            self.reportArray.add(homework)

            DispatchQueue.main.async(execute: {

                self.tableView.reloadData()
                self.hud.hide(true)
            })
            
        }
        
        
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 40
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        return 50
        
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        if section == 0 {
            return header1
        }else{
            return header2
        }
        
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            return headArray.count
        }else{
            return reportArray.count
        }
        
        
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
        
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellIdentifier = "DayReportTableViewCellIdentifier";
        
        let cell: DayReportTableViewCell = DayReportTableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: cellIdentifier)
        
        cell.selectionStyle = UITableViewCellSelectionStyle.none;
        switch (indexPath as NSIndexPath).section {
        case 0:
            let row = (indexPath as NSIndexPath).row
            let title = headArray.object(at: row) as! String
            cell.title.text = title
            break
        case 1:
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
            let row = (indexPath as NSIndexPath).row
            let title = reportArray.object(at: row) as! String
            cell.title.text = title
            
            break
        default:
            break
        }
        return cell
        
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if (indexPath as NSIndexPath).section == 1 {
            switch (indexPath as NSIndexPath).row {
            case 0:
                let classwork = ClassWorkViewController()
                self.navigationController?.pushViewController(classwork, animated: true)
                break
            case 1:
                let notice = NoticeViewController()
                self.navigationController?.pushViewController(notice, animated: true)
                break
            case 2:
                let hornor = HonorViewController()
                self.navigationController?.pushViewController(hornor, animated: true)
                break
            case 3:
                let growth = GrowthViewController()
                self.navigationController?.pushViewController(growth, animated: true)
                break
            case 4:
                let leave = LeaveViewController()
                self.navigationController?.pushViewController(leave, animated: true)
                break
            case 5:
                let homework = HomeWorkViewController()
                self.navigationController?.pushViewController(homework, animated: true)
            default:
                break
            }
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
